package com.example.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.entity.dto.LifeTool.LifeTool;
import com.example.entity.vo.request.life.DateRequestVO;
import com.example.entity.vo.request.life.MBIRequestVO;
import com.example.entity.vo.response.life.DateResponseVO;
import com.example.entity.vo.response.life.MBIResponseVO;
import com.example.mapper.LifeToolMapper;
import com.example.service.LifeToolService;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.time.format.DateTimeParseException;
import java.time.temporal.ChronoUnit;

/**
 * 生活工具服务实现类
 *
 * @auth shanyue
 * @date 2025年07月08日20:23
 */
@Service
public class LifeToolServiceImpl extends ServiceImpl<LifeToolMapper, LifeTool> implements LifeToolService {

    /**
     * 根据path字段获取对应的生活工具对象
     *
     * @param path 工具路径
     * @return 匹配到的LifeTool对象，没有返回null
     */
    @Override
    public LifeTool getInfoByPath(String path) {
        return lambdaQuery()
                .eq(LifeTool::getPath, path)
                .one();
    }

    /**
     * 根据用户输入的身高和体重计算结果，MBI指数和健康结论
     * @param requestVO 请求体，包含用户输入的身高和体重
     * @return  封装在RestBean的MBI计算结果
     */
    @Override
    public MBIResponseVO getMBIResult(MBIRequestVO requestVO) {
        try {
            //将输出的身高从厘米转成米
            BigDecimal height = new BigDecimal(requestVO.getHeight()).divide(new BigDecimal("100"),2,RoundingMode.HALF_UP);
            BigDecimal weight = new BigDecimal(requestVO.getWeight());

            //计算MBI
            //身高的平方
            BigDecimal heightSquared = height.multiply(height);
            //体重除以身高的平凡
            BigDecimal bmi = weight.divide(heightSquared, 10, RoundingMode.HALF_UP);
            //MBI保留两位小数
            BigDecimal bmiRounded = bmi.setScale(2, RoundingMode.HALF_UP);

            //根据MBI值判断健康结论
            double bmiValue = bmiRounded.doubleValue();
            String conclusion;
            if (bmiValue < 18.5) {
                conclusion = "体重过轻";
            } else if (bmiValue < 24.9) {
                conclusion = "体重正常";
            } else if (bmiValue < 29.9) {
                conclusion = "超重";
            } else {
                conclusion = "肥胖";
            }
            //返回封装好的BMI计算结果和健康结论
            return new MBIResponseVO(bmiRounded.toPlainString(), conclusion);
        } catch (NumberFormatException e) {
            //若输入的值不合理则返回错误提示
            return new MBIResponseVO("N/A", "应输入纯数字");
        }
    }

    /**
     * 计算两个日期之间的天数差
     * @param requestVO 请求体，包含开始日期和结束日期，格式为YYYY-MM-DD
     * @return  封装在RestBean的天数差计算结果
     */
    @Override
    public DateResponseVO getDateInterval(DateRequestVO requestVO) {
        try{
            LocalDate start = LocalDate.parse(requestVO.getStart());
            LocalDate end = LocalDate.parse(requestVO.getEnd());

            //计算日期之间的天数差，取绝对值保证为正值
            long days = ChronoUnit.DAYS.between(start,end);
            String result = String.valueOf(Math.abs(days));

            return new DateResponseVO(result);
        }catch (DateTimeParseException e){
            return new DateResponseVO("日期格式错误");
        }
    }
}
